import datetime
import unittest

from Autotests.BaseTest import BaseManservSOAPTestCase, PartnerAndAccountAutoCreation


@PartnerAndAccountAutoCreation
class ContactPersonsAndNotes(BaseManservSOAPTestCase):
    def __init__(self, *args, **kwargs):
        BaseManservSOAPTestCase.__init__(self, *args, **kwargs)

    def setUp(self, *args, **kwargs):
        BaseManservSOAPTestCase.setUp(self, *args, **kwargs)
        self._basePerson = {
            'partnerId': self.getPartnerForCaseInfo().id,
            'contactPersonType': ['AuthorizedSigner', 'Administrative', 'Technical', 'Sales'],
            'title': 'testTitle',
            'firstName': 'fName',
            'lastName': 'lName',
            'position': 'testPosition',
            'emailAddress': 'mail@mail.ru',
            'phoneNumber': '1234567890'
        }
        self._baseNote = {
            'partnerId': self.getPartnerForCaseInfo().id,
            'userId': self._getCurrentUserInfo().id,
            'dateTime': datetime.datetime.now().isoformat(),
            'contactPersonId': '', #must be defined
            'typeOfContact':
                {
                   'value': 'Email',
                },
            'details': 'Test Details',
            'statusOfPlanning':
                {
                    'value': 'Planned',
                },
        }

    def test_AddContactPerson(self):
        testPerson = self._basePerson.copy()
        testPerson['firstName'] = 'AddContactPerson'
        result = self.manserv.AddContactPerson(**testPerson)
        self.assertTrue(result.isnumeric())
        self.manserv.RemoveContactPerson(contactPersonId = result)

    def test_AddContactNote(self):
        contactId = self._createContact('AddContactNote')
        result = self._createNote(contactId)

        self.assertTrue(result.isnumeric())
        self.manserv.RemoveContactPerson(contactPersonId = contactId)

    def test_EnumContactNotes(self):
        contactId = self._createContact('EnumContactNotes')
        noteId = self._createNote(contactId)

        result = self.manserv.EnumerateContactNotes(partnerId = self.getPartnerForCaseInfo().id)
        self.assertEqual(len(result.detailedContactNoteInfo), 1)
        self.assertEqual(result.detailedContactNoteInfo[0].contactNote.contactPersonId, contactId)
        self.assertEqual(result.detailedContactNoteInfo[0].contactNote.id, noteId)

        self.manserv.RemoveContactPerson(contactPersonId = contactId)

    def test_EnumContactPersons(self):
        contactId = self._createContact('EnumContactPersons')

        result = self.manserv.EnumerateContactPersons(partnerId = [self.getPartnerForCaseInfo().id])
        self.assertEqual(len(result.contactPersonInfo), 1)
        self.assertEqual(result.contactPersonInfo[0].id, contactId)

        self.manserv.RemoveContactPerson(contactPersonId = contactId)

        empty = self.manserv.EnumerateContactPersons(partnerId = [self.getPartnerForCaseInfo().id])
        self.assertEqual(empty, '')

    def test_EnumLastContactNotes(self):
        contactId = self._createContact('EnumLastContactNotes')
        noteId = self._createNote(contactId)

        result = self.manserv.EnumerateLastContactNotes(partners = [self.getPartnerForCaseInfo().id])
        self.assertEqual(result.contactNoteInfo[0].contactPersonId, contactId)
        self.assertEqual(result.contactNoteInfo[0].id, noteId)
        self.assertEqual(result.contactNoteInfo[0].partnerId, self.getPartnerForCaseInfo().id)

        self.manserv.RemoveContactPerson(contactPersonId = contactId)

    def test_GetContactNoteInfo(self):
        contactId = self._createContact('GetContactNoteInfo')
        noteId = self._createNote(contactId)

        result = self.manserv.GetContactNoteInfo(contactNoteId = noteId)
        self.assertEqual(result.id, noteId)
        self.assertEqual(result.contactPersonId, contactId)

        self.manserv.RemoveContactPerson(contactPersonId = contactId)

    def test_GetContactPersonInfoById(self):
        contactId = self._createContact('GetContactPersonInfoById')
        result = self.manserv.GetContactPersonInfoById(contactPersonId = contactId)
        self.assertEqual(result.id, contactId)
        self.manserv.RemoveContactPerson(contactPersonId = contactId)

    def test_ModifyContactNote(self):
        contactId = self._createContact('ModifyContactNote')
        noteId = self._createNote(contactId)

        modifiedNote = self._baseNote.copy()
        modifiedNote['contactPersonId'] = contactId
        modifiedNote['id'] = noteId
        modifiedNote['details'] = 'A new detail'
        modifiedNote['typeOfContact'] = {'value': 'Phone'}
        modifiedNote['statusOfPlanning'] = {'value': 'Done'}

        result = self.manserv.ModifyContactNote(**modifiedNote)
        self.assertIsNone(result)

        noteInfo = self.manserv.GetContactNoteInfo(contactNoteId = noteId)
        self.assertEqual(noteInfo.details, 'A new detail')
        self.assertEqual(noteInfo.typeOfContact, 'Phone')
        self.assertEqual(noteInfo.statusOfPlanning, 'Done')

        self.manserv.RemoveContactPerson(contactPersonId = contactId)

    def test_ModifyContactPerson(self):
        contactId = self._createContact('ModifyContactPerson')
        modifiedPerson = self._basePerson.copy()
        modifiedPerson['id'] = contactId
        modifiedPerson['firstName'] = 'newName'

        result = self.manserv.ModifyContactPerson(**modifiedPerson)
        self.assertIsNone(result)

        personInfo = self.manserv.GetContactPersonInfoById(contactPersonId = contactId)
        self.assertEqual(personInfo.firstName, 'newName')

        self.manserv.RemoveContactPerson(contactPersonId = contactId)

    def test_RemoveContactNote(self):
        contactId = self._createContact('RemoveContactNote')
        noteId = self._createNote(contactId)

        count = self._getNotesCount(self.getPartnerForCaseInfo().id)

        result = self.manserv.RemoveContactNote(contactNoteId = noteId)
        self.assertIsNone(result)

        countAfter = self._getNotesCount(self.getPartnerForCaseInfo().id)
        self.assertEqual(count - 1, countAfter)

    def test_RemoveContactPerson(self):
        contactId = self._createContact('RemoveContactPerson')
        count = self._getPersonsCount(self.getPartnerForCaseInfo().id)
        result = self.manserv.RemoveContactPerson(contactPersonId = contactId)
        self.assertIsNone(result)
        countAfter = self._getPersonsCount(self.getPartnerForCaseInfo().id)
        self.assertEqual(count - 1, countAfter)

    def test_EnumAllContactPersons(self):
        contactId = self._createContact('RemoveContactPerson')
        result = self.manserv.EnumerateAllContactPersons(partnerId = self.getPartnerForCaseInfo().id,
                                                         contactPersonType = ['Sales'])

        self.assertEqual(len(result.contactPersonInfo), 1)
        self.assertEqual(result.contactPersonInfo[0].partnerId, self.getPartnerForCaseInfo().id)
        self.assertEqual(result.contactPersonInfo[0].id, contactId)

        self.manserv.RemoveContactPerson(contactPersonId = contactId)

    def _getCurrentUserInfo(self):
        partnerName, userName, password = self.manserv.getLoginParams()
        partnerId = self.manserv.GetPartnerInfo(name = partnerName).id
        return self.manserv.GetUserInfo(partnerId = partnerId, name = userName, password = password)

    def _getPersonsCount(self, partnerId):
        result = self.manserv.EnumerateContactPersons(partnerId = partnerId)
        if result == '':
            return 0
        return len(result.contactPersonInfo)

    def _getNotesCount(self, partnerId):
        result = self.manserv.EnumerateContactNotes(partnerId = partnerId)
        if result == '':
            return 0
        return len(result.detailedContactNoteInfo)

    def _createContact(self, contactName):
        testPerson = self._basePerson.copy()
        testPerson['firstName'] = contactName
        return self.manserv.AddContactPerson(**testPerson)

    def _createNote(self, contactId):
        testNote = self._baseNote.copy()
        testNote['contactPersonId'] = contactId
        testNote['dateTime'] = datetime.datetime.now().isoformat()
        return self.manserv.AddContactNote(**testNote)

if __name__ == "__main__":
    unittest.main()